package com.zkthink.ceres.product.converter;

import com.alibaba.fastjson.JSON;
import com.zkthink.dozer.DozerUtils;
import com.zkthink.ceres.product.dto.ChargeDetailDTO;
import com.zkthink.ceres.product.dto.LogisticsDetailDTO;
import com.zkthink.ceres.product.dto.LogisticsRegionDTO;
import com.zkthink.ceres.product.entity.Logistics;
import com.zkthink.ceres.product.entity.LogisticsDetail;
import com.zkthink.ceres.product.vo.LogisticsDetailVO;
import com.zkthink.ceres.product.vo.LogisticsVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *   物流信息bean 转换器
 * </p>
 *
 * @author vivi.huang
 * @since 2020/5/16
 */
@Component
@AllArgsConstructor
public class LogisticsBeanConverter {

    private DozerUtils dozerUtils;

    public List<LogisticsDetail> covertToLogisticsDetailList(Long logisticsId, List<LogisticsDetailDTO> logisticsDetailDTOList) {
        return logisticsDetailDTOList.stream().map(dto -> {
            LogisticsDetail logisticsDetail = dozerUtils.map(dto, LogisticsDetail.class);
            logisticsDetail.setChargeDetail(JSON.toJSONString(dto.getChargeDetailDTO()));
            logisticsDetail.setRegion(JSON.toJSONString(dto.getRegionList()));
            logisticsDetail.setLogisticsId(logisticsId);
            logisticsDetail.setIsDelete(0);
            return logisticsDetail;
        }).collect(Collectors.toList());
    }

    public LogisticsVO convertToLogisticsVO(Logistics logistics, List<LogisticsDetail> logisticsDetailList) {
        LogisticsVO logisticsVO = dozerUtils.map(logistics, LogisticsVO.class);
        logisticsVO.setLogisticsDetailDTOList(this.covertToLogisticsDetailVOList(logisticsDetailList));
        return logisticsVO;
    }

    private List<LogisticsDetailVO> covertToLogisticsDetailVOList(List<LogisticsDetail> logisticsDetailList) {
        return logisticsDetailList.stream().map(l -> {
            LogisticsDetailVO logisticsDetailVO = dozerUtils.map(l, LogisticsDetailVO.class);
            logisticsDetailVO.setRegionList(JSON.parseArray(l.getRegion(), String.class));
            logisticsDetailVO.setChargeDetailDTO(JSON.parseObject(l.getChargeDetail(), ChargeDetailDTO.class));
            return logisticsDetailVO;
        }).collect(Collectors.toList());
    }

    public List<LogisticsDetailDTO> convertToLogisticsDetailDTOList(List<LogisticsDetail> logisticsDetailList) {
        return logisticsDetailList.stream().map(l -> {
            LogisticsDetailDTO logisticsDetailDTO = dozerUtils.map(l, LogisticsDetailDTO.class);
            logisticsDetailDTO.setLogisticsRegionDTOList(this.convertToLogisticsRegionDTO(l.getRegion()));
            logisticsDetailDTO.setChargeDetailDTO(JSON.parseObject(l.getChargeDetail(), ChargeDetailDTO.class));
            return logisticsDetailDTO;
        }).collect(Collectors.toList());
    }

    private List<LogisticsRegionDTO> convertToLogisticsRegionDTO(String region) {
        List<String> regionList = JSON.parseArray(region, String.class);
        return regionList.stream().map(r -> new LogisticsRegionDTO(r)).collect(Collectors.toList());

    }
}